<?php
namespace app\beautiful\controller\admin\mall\pintuan;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\logic\PinTuanLogic;
use app\beautiful\model\mall\PinTuanGroupModel;
use app\beautiful\model\order\PinTuanOrderModel;
use think\facade\Db;

class Group extends BaseController{
    public function index()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
        ];
        $status=$this->request->param('status/d',0);
        if(!empty($status)) $where[]=['status','=',$status];

        $pin_tuan_group_model=new PinTuanGroupModel();
        $pin_tuan_group_model=$pin_tuan_group_model->with(['chief','pin_tuan_goods'=>function($query){
            $query->with(['goods']);
        }]);
        $order=['add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($pin_tuan_group_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function getGroupMemberList()
    {
        $pin_tuan_group_id = $this->request->param('pin_tuan_group_id/d',0);
        if($pin_tuan_group_id==0) return $this->error('拼团不存在');

        if(null === $pin_tuan_group = PinTuanGroupModel::find($pin_tuan_group_id)) return $this->error('拼团不存在');
        if($pin_tuan_group->shop_id != $this->request->shopId) return $this->error('拼团不存在');

        $where=[
            ['shop_id','=',$this->request->shopId],
            ['pin_tuan_group_id','=',$pin_tuan_group_id],
            ['status','not in',[1,-3]]
        ];

        $pin_tuan_order_model=new PinTuanOrderModel();
        $order=['status'=>'desc','add_time'=>'desc'];
        $field="*";
        $append=['add_time_format','status_means'];
        $return = DataLogic::getDataList($pin_tuan_order_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }

    public function getGroupDetail()
    {
        $pin_tuan_group_id=$this->request->param('pin_tuan_group_id/d',0);
        if(null === $pin_tuan_group = PinTuanGroupModel::find($pin_tuan_group_id))  return $this->error('拼团不存在');
        if($pin_tuan_group->shop_id != $this->request->shopId) return $this->error('拼团不存在');
        if($pin_tuan_group->status!=1)  return $this->error('已成团或者成团失败了');
        $info=[
            'remain_num'=>$pin_tuan_group->group_size-$pin_tuan_group->num,
        ];
        return $this->success(['info'=>$info]);
    }

    public function toGroup()
    {
        $pin_tuan_group_id=$this->request->param('pin_tuan_group_id/d',0);
        if(null === $pin_tuan_group = PinTuanGroupModel::find($pin_tuan_group_id))  return $this->error('拼团不存在');
        if($pin_tuan_group->shop_id != $this->request->shopId) return $this->error('拼团不存在');
        if($pin_tuan_group->status!=1)  return $this->error('已成团或者成团失败了');

        $remain_num=$pin_tuan_group->group_size-$pin_tuan_group->num;
        if($remain_num<=0) return $this->success('人数已购无法添加');

        $datas=$this->request->param('datas/a',[]);
        if(empty($datas) || !is_array($datas)) return $this->error('请传入数据');
        if(count($datas) != $remain_num) return $this->error('传输的数据过多或者过少');
        $pin_tuan_order=PinTuanOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_chief','=',1],
            ['pin_tuan_group_id','=',$pin_tuan_group_id],
            ['status','=',2]
        ])->find();
        if(null === $pin_tuan_order) return $this->error('团长不存在');
        Db::startTrans();
        try{
            foreach ($datas as $k => $v){
                if(empty($v['member_name']) || empty($v['member_face'])){
                    Db::rollback();
                    return $this->error("第".($k+1)."组数据有缺失");
                }
                $datas[$k]['shop_id']=$this->request->shopId;
                $datas[$k]['logistics_type']=$pin_tuan_order->logistics_type;
                $datas[$k]['pin_tuan_group_id']=$pin_tuan_group_id;
                $datas[$k]['group_size']=$pin_tuan_order->group_size;
                $datas[$k]['total_price']=$pin_tuan_order->total_price;
                $datas[$k]['need_pay']=$pin_tuan_order->need_pay;
                $datas[$k]['freight']=$pin_tuan_order->freight;
                $datas[$k]['member_coupon_id']=$pin_tuan_order->member_coupon_id;
                $datas[$k]['coupon_money']=$pin_tuan_order->coupon_money;
                $datas[$k]['use_integral']=$pin_tuan_order->use_integral;
                $datas[$k]['integral_balance']=$pin_tuan_order->integral_balance;
                $datas[$k]['pay_type']=$pin_tuan_order->pay_type;
                $datas[$k]['is_paid']=$pin_tuan_order->is_paid;
                $datas[$k]['status']=3;
                $datas[$k]['pay_time']=$this->request->time();
                $datas[$k]['receiving_name']=$pin_tuan_order->receiving_name;
                $datas[$k]['receiving_mobile']=$pin_tuan_order->receiving_mobile;
                $datas[$k]['receiving_address_province']=$pin_tuan_order->receiving_address_province;
                $datas[$k]['receiving_address_city']=$pin_tuan_order->receiving_address_city;
                $datas[$k]['receiving_address_county']=$pin_tuan_order->receiving_address_county;
                $datas[$k]['receiving_address_info']=$pin_tuan_order->receiving_address_info;
            }
            $pin_tuan_order_model=new PinTuanOrderModel();
            $pin_tuan_order_model->saveAll($datas);

            $pin_tuan_group->num=$pin_tuan_group->num + $remain_num;
            $pin_tuan_group->save();

            //成团
            $pin_tuan_logic = new PinTuanLogic($this->request->shopId);
            $pin_tuan_logic->fakeGroup($pin_tuan_group_id);

            Db::commit();
            return $this->success('操作成功');

        } catch(\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
}